/**
 * @file     missingNumber.ts
 * @brief    [268. 丢失的数字](https://leetcode.cn/problems/missing-number/description/)
 * @author   Zhu
 * @date     2022-11-05 21:42
 */

// 数学求和
function missingNumber(nums: number[]): number {
    function nSum(n: number): number {
        return ((n + 1) * n) / 2;
    }

    let sum = 0;
    for (const num of nums) {
        sum += num;
    }

    return nSum(nums.length) - sum;
}

// 数学求和--利用索引
function missingNumber(nums: number[]): number {
    let sum = nums.length;
    for (let i = 0; i < nums.length; i++) {
        sum += i - nums[i];
    }

    return sum;
}

// 异或
function missingNumber(nums: number[]): number {
    let xor = nums.length;
    for (let i = 0; i < nums.length; i++) {
        xor ^= i ^ nums[i];
    }

    return xor;
}
